package com.ruoyi.operationLog.controller;

import com.ruoyi.operationLog.domain.OperationLog;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.data.elasticsearch.core.query.IndexQueryBuilder;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/operationLog")
public class OperationLogController {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchTemplate;

    private RestTemplate restTemplate;


    /**
     * 判断不存在就创建索引
     */
    @GetMapping("/creat")
    public void createIndex(){
        try {

            IndexOperations operation = elasticsearchTemplate.indexOps(IndexCoordinates.of("operation"));
            if (!operation.exists()){
                //不存在创建
              operation.create();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 新增，
     * @param id
     */
    @GetMapping("/put")
    public void addEsContent(String id) {
        OperationLog operationLog = new OperationLog();
        operationLog.setId(1l);
        operationLog.setCreateTime(new Date());
        operationLog.setFileName("文件名称");
        operationLog.setStage("2023-2024");
        System.out.println(operationLog);
        IndexQuery indexQuery = new IndexQueryBuilder()
                .withId(operationLog.getId().toString())
                .withObject(operationLog)
                .build();
        // 插入es数据
        String index = elasticsearchTemplate.index(indexQuery, IndexCoordinates.of("operation"));
        System.out.println(index);
    }

    @GetMapping("/put1")
    public void addEsContent1(String id) {
        OperationLog operationLog = new OperationLog();
        operationLog.setCreateTime(new Date());
        operationLog.setFileName("文件名称");
        operationLog.setStage("2023-2024");
        operationLog.setCreateUser("wdf");
        System.out.println(operationLog);
        IndexQuery indexQuery = new IndexQueryBuilder()
                .withId(operationLog.getId().toString())
                .withObject(operationLog)
                .build();
        // 插入es数据
        String operation = elasticsearchTemplate.index(indexQuery, IndexCoordinates.of("operation"));
        System.out.println(operation);
    }

    @GetMapping("/put2")
    public void addEsContent2(String id) {
        OperationLog operationLog = new OperationLog();
        operationLog.setId(3l);
        operationLog.setCreateTime(new Date());
        operationLog.setFileName("文件名称");
        operationLog.setStage("2023-2024");
        operationLog.setCreateUser("wdf");
        System.out.println(operationLog);
        IndexQuery indexQuery = new IndexQueryBuilder()
                .withId(operationLog.getId().toString())
                .withObject(operationLog)
                .build();
        // 插入es数据
        String index = elasticsearchTemplate.index(indexQuery, IndexCoordinates.of("operation"));
        System.out.println(index);
    }
    /**
     * 排序查询
     * @param index
     * @return
     */
    @GetMapping("/get")
    public List<OperationLog> selectByTimeDesc(String index) {
        List<OperationLog> itemVOS = new ArrayList<>();

        try {
            NativeSearchQuery query = new NativeSearchQueryBuilder().withSort(SortBuilders.fieldSort("createTime").order(SortOrder.DESC))
                    .build();

            SearchHits<OperationLog> search = elasticsearchTemplate.search(query, OperationLog.class, IndexCoordinates.of(index));

            search.forEach((hits)->itemVOS.add(hits.getContent()));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return itemVOS;
    }

}
